﻿Imports System.Data.OleDb
Public Class editprofile
    Inherits System.Web.UI.Page

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Not IsPostBack Then
            Dim sql As String = "select * from userprofile where UserID=@f6"
            Dim conn As OleDbConnection = New OleDbConnection(ConfigurationManager.ConnectionStrings("maniaConnectionstring").ConnectionString)
            conn.Open()
            Dim cmd As OleDbCommand = New OleDbCommand(sql, conn)
            cmd.Parameters.AddWithValue("@f6", User.Identity.Name)

            Dim UI = cmd.ExecuteReader()

            UI.Read()

            If Not IsDBNull(UI("FirstName")) Then t1.Text = UI("FirstName")
            If Not IsDBNull(UI("LastName")) Then t2.Text = UI("LastName")
            If Not IsDBNull(UI("Gender")) Then t3.Text = UI("Gender")
            If Not IsDBNull(UI("DateOfBirth")) Then
                Dim userDOB As DateTime = UI("DateOfBirth")
                ddl3.SelectedValue = userDOB.Year
                ddl2.SelectedValue = userDOB.Month
                ddl1.SelectedValue = userDOB.Day
            End If
        End If
    End Sub

    Protected Sub Buttonsubmit_Click(ByVal sender As Object, ByVal e As EventArgs) Handles Buttonsubmit.Click
        Dim newFileName As String = ""


        If FileUpload.HasFile Then
            newFileName = Guid.NewGuid().ToString() & getSuffix(FileUpload.FileName)

            If FileUpload.HasFile Then
                FileUpload.SaveAs(Server.MapPath("images") & "/" & newFileName)
            End If
        End If

        Dim userDOB As DateTime = New DateTime(CInt(DDL3.SelectedValue), CInt(DDL2.SelectedValue), CInt(DDL1.SelectedValue))

        Dim sql As String
        If FileUpload.HasFile Then
            sql = "update userprofile Set FirstName=@f1,LastName=@f2,potrait=@f3,DateOfBirth=@f4,Gender=@f5 where UserID=@f6"
        Else
            sql = "update userprofile Set FirstName=@f1,LastName=@f2,DateOfBirth=@f4,Gender=@f5 where UserID=@f6"
        End If

        Dim conn As OleDbConnection = New OleDbConnection(ConfigurationManager.ConnectionStrings("maniaConnectionstring").ConnectionString)
        conn.Open()
        Dim cmd As OleDbCommand = New OleDbCommand(sql, conn)
        cmd.Parameters.AddWithValue("@f1", t1.Text)
        cmd.Parameters.AddWithValue("@f2", t2.Text)
        If FileUpload.HasFile Then cmd.Parameters.AddWithValue("@f3", "images/" & newFileName)
        cmd.Parameters.AddWithValue("@f4", userDOB.Date)
        cmd.Parameters.AddWithValue("@f5", t3.Text)
        cmd.Parameters.AddWithValue("@f6", User.Identity.Name)
        cmd.ExecuteNonQuery()
        conn.Close()
        cmd.Dispose()
        conn.Dispose()


    End Sub

    Function getSuffix(ByVal fn As String)
        Dim fileParts = fn.Split(".")
        Dim suffix = fileParts(fileParts.Length - 1)
        Return "." & suffix
    End Function
End Class
